﻿package com.ww_and_aloa.components.dropDown{	
	import com.greensock.TweenLite;
	
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.xml.*;
	
	public class DropDownMenu extends Sprite{
		// Properties
		private var container:Sprite=new Sprite();
		private var req:URLRequest=new URLRequest();
		private var txt:TextField=new TextField();
		private var txtFormat:TextFormat=new TextFormat();
		private var button:SimpleButton=new SimpleButton();
		private var _value:String;
		
		private var incomingData:Array = [];
		
		public var output:String;
		
		// Constructor
		public function DropDownMenu(data:Array,_width:int=200,_height:int=30):void{
			this.incomingData = data;
			
			container.y=_height;
			container.alpha=0;
			container.visible=false;
			addChild(container);
			
			var bg:Shape=drawObject(_width,_height);
			addChild(bg);
			
			txtFormat.bold=true;
			txtFormat.size=15;
			txtFormat.font="Verdana";
			txt.defaultTextFormat=txtFormat;
			txt.x=5;
			txt.y=3;
			txt.width=_width-10;
			txt.height=_height-6;
			txt.mouseEnabled=false;
			//txt.text="XML not loaded";
			addChild(txt);		
			
			button.hitTestState=drawObject(_width,_height);
			button.addEventListener(MouseEvent.CLICK,open);
			addChild(button);
						
			addItems();
			
			addEventListener(Event.ADDED_TO_STAGE,addedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);			
		}
		
		private function open(e:MouseEvent):void{
			if(container.alpha==0){
				container.visible=true;
				container.alpha=0;
				new TweenLite(container,.5,{alpha:1});
			}
			else{ close(); }
		}
		
		private function close():void{ 
			if(container.alpha>0)new TweenLite(container,.5,{alpha:0,onComplete:hideContainer}); 
		}
		
		private function hideContainer():void{
			container.visible=false;
		}
		
		private function stageMouseDown(e:MouseEvent):void{
			this.close();
		}
		
		private function stageMouseLeave(e:Event):void{
			this.close();
		}
		
		private function addedToStage(e:Event):void{ 
			stage.addEventListener(MouseEvent.MOUSE_DOWN,stageMouseDown); 
			stage.addEventListener(Event.MOUSE_LEAVE,stageMouseLeave);
		}
		private function removedFromStage(e:Event):void{
			stage.removeEventListener(MouseEvent.MOUSE_DOWN,stageMouseDown);
		}
		
		private function addItems():void{
			txt.text="Select";
			_value=null;
			var _w:int=this.width;
			var _h:int=this.height;
			
			for(var i:uint=0;i < incomingData.length;i++){
				var menuItem:MenuItem=new MenuItem(incomingData[i],incomingData[i],_w,_h);
				
				menuItem.addEventListener(MouseEvent.CLICK,menuItemClick);
				container.addChild(menuItem);
			}
			container.x=(this.width-container.width)*.5;
		}
		
		private function menuItemClick(e:MouseEvent):void{
			if(e.currentTarget.value!=_value && e.currentTarget.caption!=txt.text){
				dispatchEvent(new DropDownMenuEvent(DropDownMenuEvent.CHANGE,e.currentTarget.caption,e.currentTarget.value));
				_value=e.currentTarget.value;
				txt.text=e.currentTarget.caption;
				
				output = _value;
			}
		}
		
		private function drawObject(_width:int,_height:int):Shape{
			var newShape:Shape = new Shape();
			var colours:Array=[0xA6A6A6,0x515151];
			var alphas:Array=[1,1];
			var ratios:Array=[0,255];
			var radians:Number=90*(Math.PI/180);
			var matrix:Matrix=new Matrix();
			matrix.createGradientBox(_width,_height,radians);
			newShape.graphics.beginGradientFill(GradientType.LINEAR,colours,alphas,ratios,matrix);
			newShape.graphics.drawRoundRect(0,0,_width,_height,5,5);
			return newShape;
		}
		
		// Getters & Setters
		public function get caption():String{
			return txt.text;
		}
		public function get value():String{
			return _value;
		}
	}
}